package com.liru.chat_demo.controller;

import com.liru.chat_demo.model.FunctionCallRequest;
import com.liru.chat_demo.model.FunctionSchema;
import com.liru.chat_demo.service.McpClientService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * Controller for LLM tool operations
 */
@RestController
@RequestMapping("/llm")
public class LlmToolController {
    // MCP客户端服务，用于与MCP协议进行交互
    private final McpClientService mcpClientService;

    // 构造函数注入McpClientService实例
    public LlmToolController(McpClientService mcpClientService) {
        this.mcpClientService = mcpClientService;
    }

    // GET请求处理器，用于获取所有函数模式定义
    @GetMapping("/functions")
    public ResponseEntity<List<FunctionSchema>> getFunctions() {
        // 调用服务获取函数模式列表并返回
        return ResponseEntity.ok(mcpClientService.getFunctionSchemas());
    }

    // POST请求处理器，用于调用指定的函数
    @PostMapping("/call")
    public String callFunction(@RequestBody FunctionCallRequest request) {
        // 调用MCP客户端服务执行工具调用，成功时返回结果，失败时返回错误信息
        return mcpClientService.callTool(request.getToolName(), request.getArguments());
    }
}